/*Upload*/

#include "threadPool.h"

int recvn(int sockfd, void *buf, int length)
{
    int total = 0;
    int ret;
    char *p = (char *)buf;
    while (total < length)
    {
        ret = recv(sockfd, p + total, length - total, 0);
        total += ret;
    }
    return 0;
}

int recvFile(int netfd, char *md5, off_t offset)
{
    char orimd5[50] = {0};
    strcpy(orimd5, md5);

    train_t train;
    bzero(&train, sizeof(train));
    strcat(filename, ".filepart");
    int fd = open(filename, O_RDWR | O_CREAT, 0666);
    off_t filesize;
    recvn(netfd, &train.length, sizeof(train.length));
    recvn(netfd, &filesize, train.length);
    printf("filesize = %ld\n", filesize);

    lseek(fd, offset, SEEK_CUR);
    while (1)
    {
        bzero(&train, sizeof(train));
        recvn(netfd, &train.length, sizeof(train.length)); // 先收四个字节
        if (train.length != 1000)
        {
            printf("train.length = %d\n", train.length);
        }
        if (train.length == 0)
        {
            break;
        }
        recvn(netfd, train.data, train.length); // 再一个train.length的长度
        write(fd, train.data, train.length);
    }
    //printf("100.00%%\n");
    rename(filename, oriFilename);
    close(fd);
    return 0;
}

int upload(int netfd, char *filename, off_t offset, char* path, char* usename, MYSQL* db)
{
    //printf("entry server upload\n");
    char md5[33] = {0};
    train_t train;
    //printf("-------server before flag1 ------");
    bzero(&train, sizeof(train));
    recvn(netfd, &train.length, sizeof(train.length));
    recvn(netfd, md5, train.length);
    puts(md5);
    //printf("-------server before flag2 ------");
    char sql[1024] = {0};

    sprintf(sql,"select md5 from vftable where md5 = '%s' and username = '%s' and fileType = '1' and tomb = '0';", md5, usename); 
    printf("sql = %s\n", sql);

    int qret = mysql_query(db, sql);
    if(qret != 0)
    {
        printf("SQL error:%s\n", mysql_error(db));
        return -1;
    }

    MYSQL_RES *table = mysql_store_result(db);
    printf("mysql_num_rows(table) = %lld\n", mysql_num_rows(table));
    //文件不在实文件表里，需要上传
    //flag
    char flag[1] = {0};
    if(mysql_num_rows(table) == 0)
    {
        flag[0] = 'y';
        bzero(&train, sizeof(train));                                        
        train.length = sizeof(flag);
        memcpy(train.data, flag, train.length);
        //printf("server upload: flag = %s\n", flag);
        //printf("-----------\n");     
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        recvFile(netfd, md5, offset);
    }
    else
    {
        flag[0] = 'n';
        bzero(&train, sizeof(train));                                        
        train.length = sizeof(flag);
        memcpy(train.data, flag, train.length);
        //printf("server upload: flag = %s\n", flag);
        //printf("-----------\n");     
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
    }
    printf("unload: md5 = %s\n", md5);
    sprintf(sql,"insert into vftable (md5, username, path, fileType, preDir, tomb, filename) values ('%s', '%s', '%s', '1', '%s', '0', '%s');", md5, usename, path, path, filename);
    printf("sql = %s\n", sql);
    qret = mysql_query(db, sql);
    if(qret != 0)
    {
        printf("SQL error:%s\n", mysql_error(db));
        return -1;
    }
    //MYSQL_ROW row;
    //while((row = mysql_fetch_row(table)) != NULL){
    //    for(unsigned int i = 0; i < mysql_num_fields(table); ++i){
    //       strcat(md5, row[i]); // 取出表里存的md5值
    //    }
    //}
}
